package com.example.shushiyunsst.fragment.main.home.child.miner22

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.fastjson.JSON
import com.example.library_common.album.view.grid_view.adapter.CommAdapter
import com.example.library_common.album.view.grid_view.adapter.XQuickViewHolder
import com.example.library_common.base.BaseDataBindingFragment
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.databinding.Miner22FragmentBinding
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.MinerPop
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.StrawberryBean
import com.example.shushiyunsst.util.*
import com.example.shushiyunsst.util.MESSAGE_PAGE
import com.example.shushiyunsst.widget.water_view.model.FloatBall
import com.example.wallet.bean.UserInfo
import com.example.wallet.db.UserInfoManager
import com.lxj.xpopup.XPopup
import com.senon.modularapp.util.JssSpannableString

@Route(path = MINER22)
class Miner22Fragment : BaseDataBindingFragment<Miner22FragmentBinding>(), View.OnClickListener {

    private var mAdapter: CommAdapter<StrawberryBean>? = null

    override fun layoutId(): Int {
        return R.layout.miner_22_fragment
    }

    private val waters = mutableListOf<FloatBall>()

    //坑数
    private val kenCount = 9

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewBinder.onViewClick = this

        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.setTitle(R.string.strawberry_field)
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        mAdapter =
            object : CommAdapter<StrawberryBean>(_mActivity, R.layout.miner_item_22_fragment) {
                override fun convert(
                    holder: XQuickViewHolder?,
                    item: StrawberryBean?,
                    position: Int
                ) {
                    holder?.setVisible22(R.id.icon, (item?.isShow ?: false))
                }

            }

        viewBinder.kongXGridLayout.setAdapter(mAdapter, lifecycle)

        viewBinder.mWaterView.setOnItemClickListener { _, ball ->
            if (ball.tag == null) {
                return@setOnItemClickListener
            }
            val pop = ball.tag as MinerPop
            val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
            val map = mapOf(
                "releaseId" to pop.releaseId,
                "uid" to userInfo?.uid
            )
            RestClient.builder()
                .url(R.string.getCml)
                .raw(JSON.toJSONString(map))
//                .success {
//
//                }.failure {
//
//                }.error { _, _ ->
//
//                }
                .build()
                .post()
        }


        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo

        val map = mapOf(
            "uid" to userInfo?.uid
        )

        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.miningPoolIndex)
            .success {
                parseMiningPoolIndex(it)
            }.build().post()

    }

    private fun parseMiningPoolIndex(it: String?) {
        val obj = JSON.parseObject(it)
        var cmlNums = obj.getIntValue("cmlNums")
        cmlNums = if (cmlNums <= 0) -1 else cmlNums
//        val cmlNums = 3
//
        val unGetReleaseLog = obj["unGetReleaseLog"]
        val unGetReleaseLogStr = unGetReleaseLog?.toString() ?: ""
        val array = JSON.parseArray(unGetReleaseLogStr, MinerPop::class.java)
        val tag="可领取"
        for (e in array) {
            val str3 = "${e.releaseNums}个"
            val str4 = e.tokenType
            val span = JssSpannableString(_mActivity, "$str3\n$str4\n$tag")
                .first(str3)
                .textColor(R.color.text_color_15)
                .size(12)
                .first(str4)
                .textColor(R.color.text_color_15)
                .size(12)
                .first(tag)
                .textColor(R.color.text_color_15)
                .size(14)
            val ball = FloatBall(span)
            ball.tag = e
            waters.add(ball)
        }
        viewBinder.mWaterView.setDatas(waters)


        val cmlNumls = mutableListOf<StrawberryBean>()
        for (i in 0..kenCount) {
            val bean = StrawberryBean()
            cmlNumls.add(bean)
            bean.isShow = i < cmlNums
        }
        mAdapter?.addAll(cmlNumls)
    }

//    override fun onLazyInitView(savedInstanceState: Bundle?) {
//        super.onLazyInitView(savedInstanceState)
//        val userInfoManager = UserInfoManager.userInfoManager
//        val userInfoWrapper = userInfoManager.getUserInfoWrapper()
//        val userInfo = userInfoWrapper?.userInfo
//        if (userInfo?.checkStatusIdcard == UserInfo.unSubmit
//            || userInfo?.checkStatusIdcard == UserInfo.checkedNot
//        ) {
//            XPopup.Builder(_mActivity)
//                .enableDrag(false)
////                    .dismissOnTouchOutside(false)
////                    .dismissOnBackPressed(false)
//                .asCustom(
//                    NeedIdentifyVerifyPop(_mActivity)
//                        .setToIdentityListener(object : NeedIdentifyVerifyPop.ToIdentityListener {
//                            override fun onIdentity() {
//                                ARouter.getInstance()
//                                    .build(IDENTIFY_VERIFY)
//                                    .startNavigation(this@Miner22Fragment)
//                            }
//
//                        })
//                )
//                .show()
//        }
//    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.subAccount -> {
                start(SubAccountListFragment.newInstance())
            }
            R.id.mu_bi -> {
                ARouter.getInstance()
                    .build(MOTHER_ICON)
                    .startNavigation(this)
            }

            R.id.jie_dian->{
                ARouter.getInstance()
                    .build(ICON_NODE)
                    .startNavigation(this)
            }

            R.id.toCount -> {
                start(ToCountFragment.newInstance())
            }
            R.id.share -> {
                start(Miner22ShareFragment.newInstance())
            }
            R.id.gongYue -> {
                start(Miner22HelperFragment.newInstance())
            }
        }
    }

//    override fun onSupportVisible() {
//        super.onSupportVisible()
//
//    }


}